package com.gpay.erp.web.control;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.gpay.erp.services.common.GlobalStatus;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.support.RequestContext;
import com.gpay.erp.core.dao.page.PageHelper;
import com.gpay.erp.core.dao.page.PagerModel;
import com.gpay.erp.core.system.bean.Dept;
import com.gpay.erp.core.system.bean.SysUser;
import com.gpay.erp.services.system.DeptInteface;
import com.gpay.erp.web.util.WebUtil;
import com.gpay.erp.web.vo.ResponseInfo;

/**
 * 部门信息管理
 * 
 * @author lg
 * 
 */
@Controller
@RequestMapping("/deptController/")
public class DeptController extends BaseController {
	private Log log = LogFactory.getLog(DeptController.class);
	@Autowired
	private DeptInteface deptService;
	@Autowired
	private GlobalStatus globalStatus;
	// 页面跳转 部门管理
	@RequestMapping("deptSelect")
	public String deptSelect() {
		return "system/dept/deptSelect";
	}

	// 页面跳转 部门新增
	@RequestMapping("deptAdd")
	public String deptAdd() {
		return "system/dept/deptAdd";
	}

	// 页面跳转 部门修改
	@RequestMapping("deptUpdate")
	public String deptUpdate(String id, ModelMap modelMap) {
		modelMap.addAttribute("deptId", id);
		return "system/dept/deptUpdate";
	}
	// 部门状态
	@RequestMapping("deptGlobalStatus")
	public void deptGlobalStatus(HttpServletRequest request,
			HttpServletResponse response) {
		ResponseInfo responseInfo = new ResponseInfo();
		responseInfo.setResult("00010");
		responseInfo.setMessage("select.success");
		/* 获取部门状态，传递给页面 */
		responseInfo.setObject(globalStatus.getUserStatus());
		WebUtil.responseToJson(response, responseInfo);
	}
	/*
	 * 部门信息List，用于勾选的下拉框中
	 * 
	 * @param request
	 * 
	 * @param response
	 */
	@RequestMapping("deptList")
	public void selectDeptMessage(HttpServletRequest request,
			HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		List departmentsList = new ArrayList();
		departmentsList = deptService.selectList(null);
		if (departmentsList.size() > 0) {
			responseInfo.setResult("12000");
			responseInfo.setMessage(requestContext
					.getMessage("dept.res.success"));
		}
		responseInfo.setObject(departmentsList);
		WebUtil.responseToJson(response, responseInfo);
	}

	/*
	 * 点击树型菜单--部门管理，进行分页查询
	 * 
	 * @param pageHelper
	 * 
	 * @param request
	 * 
	 * @param response
	 */
	@RequestMapping("selectDepts")
	public void selectDepts(PagerModel pagerModel, Dept dept,
			PageHelper pageHelper, HttpServletRequest request,
			HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		HttpSession session = request.getSession();
		/* 获取登录的用户信息 */
		SysUser loginUser = (SysUser) session.getAttribute("loginUser");
		/* 当前页 */
		int pagenow = pageHelper.getPage();
		/* 每页显示记录数 */
		int rows = pageHelper.getRows();
		/* 分页信息填充进入PagerModel中 */
		dept.setPageSize(rows);
		dept.setFromRow((pagenow - 1) * rows);
		dept.setToRow(pagenow * rows);
		dept.setDeptId(loginUser.getDeptNo());
		/* 查询数据，返回PagerModel */
		pagerModel = deptService.selectPageList(dept);
		log.info("selectDepts end");
		if (pagerModel.getRows().size() > 0) {
			for (Object depts : pagerModel.getRows()) {
				Dept dept1 = (Dept) depts;
				int key = Integer.parseInt(dept1.getStatus().toString());
				switch (key) {
				case 0:
					dept1.setStatus(globalStatus.getUserStatus().get("0")
							.toString());
					break;
				case 1:
					dept1.setStatus(globalStatus.getUserStatus().get("1")
							.toString());
					break;
				}}
			responseInfo.setResult("12000");
			responseInfo.setMessage(requestContext.getMessage("dept.res.success"));
		}
		responseInfo.setObject(pagerModel);
		WebUtil.responseToJson(response, responseInfo);
	}

	/*
	 * 点击新增，增加部门信息
	 * 
	 * @param bankCard
	 * 
	 * @param request
	 * 
	 * @param response
	 */
	@RequestMapping("addDept")
	public void addDept(Dept dept, HttpServletRequest request,
			HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		HttpSession session = request.getSession();
		/* 获取登录的用户信息 */
		SysUser loginUser = (SysUser) session.getAttribute("loginUser");
		/* 设置日期格式 */
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		/* 获取当前是时间 */
		dept.setUpdateDate(df.format(new Date()));
		dept.setUpdateName(loginUser.getUserName());
		int id = deptService.insert(dept);// 增加部门信息操作
		if (id != 0) {
			log.info("插入记录后的id值为：" + id);
			responseInfo.setResult("00040");
			responseInfo.setMessage(requestContext.getMessage("add.success"));
		} else {
			responseInfo.setResult("00041");
			responseInfo.setMessage(requestContext.getMessage("add.fail"));
		}
		WebUtil.responseToJson(response, responseInfo);
	}

	/*
	 * 点击明细，根据ID查看部门详细信息
	 * 
	 * @param bankCard
	 * 
	 * @param request
	 * 
	 * @param response
	 */
	@RequestMapping("detailDept")
	public void detailBankcard(PagerModel pagerModel, Dept dept,
			HttpServletRequest request, HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		if (dept.getId() != null)// 获取前端传入的id,判断是否为null
		{
			pagerModel = deptService.selectById(dept.getId());// 根据ID查询明细
			responseInfo.setResult("00010");
			responseInfo
					.setMessage(requestContext.getMessage("select.success"));
		} else {
			responseInfo.setResult("00011");
			responseInfo.setMessage(requestContext.getMessage("select.fail"));
		}
		responseInfo.setObject(pagerModel);
		WebUtil.responseToJson(response, responseInfo);
	}

	/*
	 * 点击修改，修改部门信息
	 * 
	 * @param bankCard
	 * 
	 * @param request
	 * 
	 * @param response
	 */
	@RequestMapping("updateDept")
	public void updateDept(Dept dept, HttpServletRequest request,
			HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		HttpSession session = request.getSession();
		/* 获取登录的用户信息 */
		SysUser loginUser = (SysUser) session.getAttribute("loginUser");
		/* 设置日期格式 */
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		/* 获取当前是时间 */
		dept.setUpdateDate(df.format(new Date()));
		dept.setUpdateName(loginUser.getUserName());
		int id = deptService.update(dept);// 部门修改操作
		if (id == 0) {
			responseInfo.setResult("00031");
		responseInfo.setMessage(requestContext.getMessage("update.fail"));
		}else if(id==-1){
			responseInfo.setResult("00032");
			responseInfo.setMessage(requestContext.getMessage("dept.existing.staff"));
		} else {
			log.info("修改记录的id值为：" + id);
			responseInfo.setResult("00030");
			responseInfo.setMessage(requestContext.getMessage("update.success"));
		}
		WebUtil.responseToJson(response, responseInfo);
	}

	/*
	 * 点击禁用，根据id禁用部门 
	 * 
	 * @param ids[]
	 * 
	 * @param request
	 * 
	 * @param response
	 */
	@RequestMapping("disableDept")
	public void disableDept(HttpServletRequest request,
			HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		HttpSession session = request.getSession();
		/* 获取登录的用户信息 */
		SysUser loginUser = (SysUser) session.getAttribute("loginUser");
		String name=loginUser.getUserName();
		String[] ids = null;
		if (request.getParameterValues("ids[]") != null) {
			ids = request.getParameterValues("ids[]");// 接收前端传输的id值
		} else {
			ids = request.getParameterValues("ids");// 接收前端传输的id值
		}
		if (ids != null) {
			int dels = deptService.update(ids,name);// 根据传入id修改部门Status
			if (dels== 0) {
				responseInfo.setResult("00051");
				responseInfo.setMessage(requestContext
						.getMessage("disable.fail"));
			}else if(dels==-1){
				responseInfo.setResult("00032");
				responseInfo.setMessage(requestContext.getMessage("dept.existing.staff"));
			} else {
				responseInfo.setResult("00050");
				responseInfo.setMessage(requestContext
						.getMessage("disable.success"));
			}
		} else {
			responseInfo.setResult("00051");
			responseInfo.setMessage(requestContext.getMessage("disable.fail"));
		}
		WebUtil.responseToJson(response, responseInfo);
	}
	
	/*
	 * 点击删除，根据id删除部门信息 （数据库直接删除数据）
	 * 
	 * @param ids[]
	 * 
	 * @param request
	 * 
	 * @param response
	 */
	@RequestMapping("deleteDept")
	public void deleteDept(HttpServletRequest request,
			HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		HttpSession session = request.getSession();
		String[] ids = null;
		if (request.getParameterValues("ids[]") != null) {
			ids = request.getParameterValues("ids[]");// 接收前端传输的id值
		} else {
			ids = request.getParameterValues("ids");// 接收前端传输的id值
		}
		if (ids != null) {
			int dels = deptService.deletes(ids);// 根据传入id删除部门信息
			if (dels != 0) {
				responseInfo.setResult("00020");
				responseInfo.setMessage(requestContext
						.getMessage("delete.success"));
			} else {
				responseInfo.setResult("00021");
				responseInfo.setMessage(requestContext
						.getMessage("delete.fail"));
			}
		} else {
			responseInfo.setResult("00021");
			responseInfo.setMessage(requestContext.getMessage("delete.fail"));
		}
		WebUtil.responseToJson(response, responseInfo);
	}
	/*
	 * 根据部门名称deptName和上级业务单元编号companyNo查询是否有重复
	 * 
	 * @param dept
	 * 
	 * @param request
	 * 
	 * @param response
	 */
	@RequestMapping("selectIsDeptName")
	public void selectIsDeptName(PagerModel pagerModel,Dept dept, HttpServletRequest request,
			HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		/* 获取前端传入的deptName/companyNo,判断是否为null */
		if (dept.getDeptName()!= null&&dept.getCompanyNo()!=null) {
			/*过滤停用中的部门信息*/
			dept.setStatus("0");
			pagerModel = deptService.selectOne(dept);
			if(pagerModel!=null){
			responseInfo.setResult("10040");
			responseInfo.setMessage(requestContext.getMessage("data.repeat"));
			}else
			{
				responseInfo.setResult("10050");
				responseInfo.setMessage(requestContext.getMessage("data.norepeat"));
			}
		}else{
			responseInfo.setResult("10041");
			responseInfo.setMessage(requestContext.getMessage("data.receive.fail"));
		}
		WebUtil.responseToJson(response, responseInfo);
	}
	
}
